package com.jy.wms.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.UUIDUtils;
import com.jy.wms.common.annotions.BaseServiceDescribe;
import com.jy.wms.common.base.BaseService;
import com.jy.wms.common.base.BaseSessionInfo;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.common.base.SucessException;
import com.jy.wms.pojo.*;
import com.jy.wms.dao.WmsPickingMapper;
import feign.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.security.acl.Owner;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
    * @ClassName: WmsPickingService.java
 * @Description: 拣货管理/发运管理
    * @Author syh
    * @Date Wed Oct 25 18:25:38 CST 2017
    */

    @Service
    public class WmsPickingService extends BaseService<WmsPickingMapper,WmsPicking> {
        /**
        * 拣货
        * @param sshzid
        * @param userinfo
        * @param list
        * @throws ServiceException
        */
        @BaseServiceDescribe(moduleName = "拣货管理",btnName = "拣货")
        public void pickWmsicking( String sshzid,UserInfo userinfo,List<String> list) throws Exception{
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//取仓库id
            String name=userinfo.getUser().getName();//当前用户名称
            Map<String,Object> map =null;
            Map<String,Object> map2 =null;
            //判断货主管理中“是否切箱”  0不切箱 1切箱
            WmsPicking owner=dao.sechSfqx(sshzid);
            Integer jhsl=0;//主单位数量
            for (String id : list) {
                WmsShipmentOrder wmsShipmentOrder=dao.selectDd(id);
                String fyzt="1" ;
                String fyzt2="2" ;
               if(!fyzt.equals(wmsShipmentOrder.getFyzt()) && !fyzt2.equals(wmsShipmentOrder.getFyzt())){
                    throw new ServiceException("请选择SO状态为部分分配或者全部分配的发运订单数据");
               }else{
                   String jhzt="0";
                   String jhzt2="1";
                   if(!jhzt.equals(wmsShipmentOrder.getJhzt()) && !jhzt2.equals(wmsShipmentOrder.getJhzt())){
                       throw new ServiceException("请选择拣货状态为初始或者部分拣货的发运订单数据");
                   }else{
                       String cdzt="1";
                       if(!cdzt.equals(wmsShipmentOrder.getCdzt())){
                           throw new ServiceException("请选择拆单状态为已拆单的发运订单数据");
                       }else{
                           //拣货任务状态(0.初始,1.部分生成拣货任务,2.全部生成拣货任务)
                           String jhrwzt = "2";
                           if (jhrwzt.equals(wmsShipmentOrder.getJhrwzt())) {
                               throw new ServiceException("请选择拣货任务状态为初始/部分生成的发运订单数据");
                           } else {
                               //查询发运订单明细用订单id
                               map = new HashMap<>();
                               map.put("ssfyddid", wmsShipmentOrder.getId());
                               map.put("fymxzt", "");
                               List<WmsShipmentOrderDetailed> wmsShipmentOrderDetailedList = dao.searchOrderDetailed(map);
                               boolean flag = false;
                               //循环明细
                               for (WmsShipmentOrderDetailed wmsShipmentOrderDetailed : wmsShipmentOrderDetailedList) {
                                   Integer xdwys = 0;//主单位个数
                                   Integer xdwzs = 0;//箱单位总数
                                   Integer zsl = 0;//先定义一个总数量(用于和发运明细的主单位数量进行比对)
                                   //是否装箱(0不装箱 1装箱)
                                   if ("1".equals(owner.getZxqx())) {
                                       //判断是否质检
                                       if ("0".equals(wmsShipmentOrder.getZxzt())) {
                                           throw new ServiceException(wmsShipmentOrder.getFybh() + "单号数据未装箱");
                                       } else {
                                           map = new HashMap<String, Object>();
                                           //发运明细id
                                           map.put("ssfymxid", wmsShipmentOrderDetailed.getId());
                                           //货主id
                                           map.put("sshzid", sshzid);
                                           //仓库id
                                           map.put("ckid", ckid);
                                           //拣货人
                                           map.put("jhr", "0");
                                           //拣货任务生成状态 0：未生成 1：已生成
                                           map.put("sczt", "0");
                                           //拿着订单明细id去查分配表、库存的集合数据
                                           List<WmsPicking> wmsPickingList = dao.seachKc(map);
                                           WmsPicking pick = null;//生成拣货记录的对象
                                           for (WmsPicking wmsPicking : wmsPickingList) {
                                               Integer kcsl = 0;//先定义一个库存需要减去的待拣货数量
                                               //用分配的id 查装箱的集合
                                               List<WmsBox> wmsBoxList = dao.selectZxjh(wmsPicking.getSsfpid());
                                               for (WmsBox box : wmsBoxList) {
                                                   //查询到库存对象
                                                   WmsStock wmsStock = dao.selectKc(wmsPicking.getKcid());
                                                   wmsStock.setId(UUIDUtils.uuid());
                                                   wmsStock.setKwid(owner.getJhqkwid());//把库位id换成集货区库位id
                                                   wmsStock.setSl(String.valueOf(box.getSl()));//把实际装箱数量主单位 ,放到库存表的库存数量里
                                                   wmsStock.setDxjsl("0");
                                                   wmsStock.setDzysl("0");
                                                   wmsStock.setDjsl("0");
                                                   wmsStock.setPssl("0");
                                                   wmsStock.setBhsl("0");
                                                   map.put("WmsStock", wmsStock);
                                                   Integer lhkw = dao.insetNewKc(map);//生成新库存
                                                   if (lhkw > 0) {
                                                   } else {
                                                       throw new ServiceException("理货库存新增失败");
                                                   }
                                                   map2 = new HashMap<String, Object>();
                                                   pick = new WmsPicking();
                                                   pick.setId(UUIDUtils.uuid());
                                                   pick.setSsfpid(wmsPicking.getSsfpid());
                                                   pick.setSszxid(box.getId());
                                                   pick.setSsydid(wmsShipmentOrder.getId());
                                                   pick.setSsfymxid(wmsShipmentOrderDetailed.getId());
                                                   pick.setKcid(wmsPicking.getKcid());
                                                   pick.setSjsl(box.getSl());
                                                   pick.setJhkwid(wmsPicking.getJhkwid());
                                                   pick.setJihkwid(owner.getJhqkwid());
                                                   pick.setFyzt("0");
                                                   pick.setJhhkcid("0");
                                                   pick.setCkid(ckid);
                                                   pick.setJhhkcid(wmsStock.getId());
                                                   map2.put("WmsPicking", pick);
                                                   Integer jhls = dao.insertPick(map2);//添加拣货历史记录
                                                   if (jhls > 0) {
                                                       //把现在得到的总数,到库存表里用待下架数量和库存数量各自减掉去
                                                       map = new HashMap<String, Object>();
                                                       map.put("kcid", wmsPicking.getKcid());
                                                       map.put("kcsl", box.getSl());
                                                       Integer kcdj = dao.updateSl(map);
                                                       if (kcdj > 0) {
                                                       } else {
                                                           throw new ServiceException("库存数量更改失败");
                                                       }
                                                   } else {
                                                       throw new ServiceException("拣货历史记录添加失败");
                                                   }
                                                   Integer sl = Integer.valueOf(box.getSl());//箱里的主单位数量,也就是实际数量
                                                   kcsl = sl + kcsl;//(一条分配对应一条库存,多个装箱的总数量) 循环叠加(把装箱里的实际数量循环取出来和定义的总数)
                                               }
                                               map.put("name", name);
                                               map.put("ssfpid", wmsPicking.getSsfpid());
                                               Integer fpjhr = dao.updateFp(map);//更改分配拣货人
                                               if (fpjhr > 0) {
                                               } else {
                                                   throw new ServiceException("分配数据拣货人更改失败");
                                               }
                                               Integer zxjhr = dao.updateZh(map);//更改装箱拣货人
                                               if (zxjhr > 0) {
                                               } else {
                                                   throw new ServiceException("装箱数据拣货人更改失败");
                                               }
                                               zsl = kcsl + zsl;//全部分配对应装箱的主单位数量,(一条明细对应全部装箱的主单位数量)zsl是最终用于判断的全部拣货还是部分拣货
                                           }
                                       }
                                   } else if ("0".equals(owner.getZxqx())) {
                                       WmsPicking picking = null;//生成拣货记录的对象
                                       //拿着订单明细id 查分配表和库存的 联合集合数据
                                       map = new HashMap<String, Object>();
                                       map.put("ssfymxid", wmsShipmentOrderDetailed.getId());
                                       map.put("sshzid", sshzid);
                                       map.put("ckid", ckid);
                                       map.put("jhr", "0");
                                       //拣货任务生成状态 0：未生成 1：已生成
                                       map.put("sczt", "0");
                                       List<WmsPicking> wmsPickingList2 = dao.seachKc_s(map);
                                       for (WmsPicking wmsPicking : wmsPickingList2) {
                                           map2 = new HashMap<String, Object>();
                                           Integer sl = Integer.valueOf(wmsPicking.getFpsl());//箱里的主单位数量,也就是实际数量
                                           zsl = sl + zsl;//循环叠加(把箱里的实际数量循环取出来和定义的总数),zsl这个总数用于判断是全拣货还是部分拣货的
                                           //查询到库存对象
                                           WmsStock wmsStock = dao.selectKc(wmsPicking.getKcid());
                                           wmsStock.setId(UUIDUtils.uuid());
                                           wmsStock.setKwid(owner.getJhqkwid());//把库位id换成集货区库位id
                                           wmsStock.setSl(String.valueOf(sl));//把实际装箱数量主单位 ,放到库存表的库存数量里
                                           wmsStock.setDxjsl("0");
                                           wmsStock.setDzysl("0");
                                           wmsStock.setDjsl("0");
                                           wmsStock.setPssl("0");
                                           wmsStock.setBhsl("0");
                                           map2.put("WmsStock", wmsStock);
                                           Integer lhkw = dao.insetNewKc(map2);//生成新库存
                                           if (lhkw > 0) {
                                           } else {
                                               throw new ServiceException("理货库存新增失败");
                                           }
                                           picking = new WmsPicking();
                                           picking.setId(UUIDUtils.uuid());
                                           picking.setSsfpid(wmsPicking.getSsfpid());
                                           picking.setSszxid("0");
                                           picking.setSsydid(wmsShipmentOrder.getId());
                                           picking.setSsfymxid(wmsShipmentOrderDetailed.getId());
                                           picking.setKcid(wmsPicking.getKcid());
                                           picking.setSjsl(wmsPicking.getFpsl());
                                           picking.setJhkwid(wmsPicking.getJhkwid());
                                           picking.setJihkwid(owner.getJhqkwid());
                                           picking.setFyzt("0");
                                           picking.setJhhkcid("0");
                                           picking.setCkid(ckid);
                                           picking.setJhhkcid(wmsStock.getId());
                                           map2.put("WmsPicking", picking);
                                           Integer jhls = dao.insertPick(map2);//添加拣货历史记录
                                           if (jhls > 0) {
                                               map2.put("kcid", wmsPicking.getKcid());
                                               map2.put("kcsl", sl);
                                               Integer kcdj = dao.updateSl(map2);
                                               if (kcdj > 0) {
                                               } else {
                                                   throw new ServiceException("库存数量更改失败");
                                               }
                                           } else {
                                               throw new ServiceException("拣货历史记录添加失败");
                                           }
                                           map2.put("name", name);
                                           map2.put("ssfpid", wmsPicking.getSsfpid());
                                           Integer fpjhr = dao.updateFp(map2);//更改分配拣货人
                                           if (fpjhr > 0) {
                                           } else {
                                               throw new ServiceException("分配数据拣货人更改失败");
                                           }
                                       }
                                   } else {
                                       throw new ServiceException("货主是否装箱状态有误");
                                   }
                                   // 用货品id查这个箱里的货品对应的包装明细类型为2的箱数量//在货品放这个箱里能放多少个
                                   map = new HashMap<String, Object>();
                                   map.put("hpid", wmsShipmentOrderDetailed.getHpid());
                                   map.put("bzlx", "2");
                                   WmsPackingDetailed detailed = dao.searchPackingDetailed(map);
                                   map.put("bzlx", "0");
                                   WmsPackingDetailed detailed2 = dao.searchPackingDetailed(map);
                                   //箱主单位整数（箱数）
                                   xdwzs = zsl / Integer.parseInt(detailed.getZdwsl());
                                   //箱主单位余数（个数）
                                   xdwys = zsl % Integer.parseInt(detailed.getZdwsl());
                                   String jhxdwsl = xdwzs + detailed.getMs() + xdwys + detailed2.getMs();
                                   Integer mxfyzt;
                                   map = new HashMap<String, Object>();
                                   map.put("id", wmsShipmentOrderDetailed.getId());
                                   map.put("jhxdwsl", jhxdwsl);
                                   map.put("zsl", zsl);
                                   //把现在得到的总数,拿着和订单明细里的数量进行比对,如果相等是全部检出,如果小于订单明细里的数量是部分检出
                                   Integer fpsl = Integer.parseInt(wmsShipmentOrderDetailed.getFpsl());
                                   if (zsl.equals(fpsl)) {
                                       map.put("fymxzt", "4");
                                   } else {
                                       flag = true;
                                       map.put("fymxzt", "3");
                                   }
                                   //改发运明细状态
                                   mxfyzt = dao.updateFymxzt(map);
                                   if (mxfyzt > 0) {
                                   } else {
                                       throw new ServiceException("发运明细状态更改失败");
                                   }
                               }
                               Integer ddfyzt;
                               map = new HashMap<String, Object>();
                               map.put("id", wmsShipmentOrder.getId());
                               if (flag == false) {
                                   map.put("jhzt", "2");
                               } else {
                                   map.put("jhzt", "1");
                               }
                               //发运订单里的拣货状态
                               ddfyzt = dao.updateFy(map);
                               if (ddfyzt > 0) {
                               } else {
                                   throw new ServiceException("发运状态更改失败");
                               }
                           }
                       }
                   }
               }
            }
            throw new SucessException("拣货成功");
        }

    /**
     * 追加拣货
     * @param sshzid
     * @param userinfo
     * @param list
     * @throws ServiceException
     */
    @BaseServiceDescribe(moduleName = "追加拣货管理",btnName = "追加拣货")
    public void AddPicking( String sshzid,UserInfo userinfo,List<String> list) throws Exception {
       String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//取仓库id
        String name = userinfo.getUser().getName();//当前用户名称
        Map<String, Object> map = null;
        Map<String, Object> map2 = null;
        //判断货主管理中“是否切箱” zxqx 0不切箱 1切箱,  dcfh是否允许多次发货 0不允许 1允许 默认1
        WmsPicking owner = dao.sechSfqx(sshzid);
        if ("1".equals(owner.getDcfh())) {
        for (String id : list) {
            WmsShipmentOrder wmsShipmentOrder = dao.selectDd(id);
                String jhzt = "1";
                if (!jhzt.equals(wmsShipmentOrder.getJhzt())) {
                    throw new ServiceException("请选择拣货状态为部分拣货的发运订单数据");
                } else {
                    //查询发运订单明细用订单id
                     map=new HashMap<>();
                     map.put("ssfyddid",wmsShipmentOrder.getId());
                     map.put("fymxzt","3");
                    List<WmsShipmentOrderDetailed> wmsShipmentOrderDetailedList = dao.searchOrderDetailed(map);
                    boolean flag = false;
                    //循环明细
                    for (WmsShipmentOrderDetailed wmsShipmentOrderDetailed : wmsShipmentOrderDetailedList) {
                        Integer xdwys = 0;//主单位个数
                        Integer xdwzs = 0;//箱单位总数
                        Integer zsl = 0;//先定义一个总数量(用于和发运明细的主单位数量进行比对)
                        if ("1".equals(owner.getZxqx())) {
                            map = new HashMap<String, Object>();
                            map.put("ssfymxid", wmsShipmentOrderDetailed.getId());
                            map.put("sshzid", sshzid);
                            map.put("ckid", ckid);
                            map.put("jhr", "0");
                            //拿着订单明细id去查分配表、库存的集合数据
                            List<WmsPicking> wmsPickingList = dao.seachKc(map);
                            WmsPicking pick = null;//生成拣货记录的对象
                            for (WmsPicking wmsPicking : wmsPickingList) {
                                Integer kcsl = 0;//先定义一个库存需要减去的待拣货数量
                                //用分配的id 查装箱的集合
                                List<WmsBox> wmsBoxList = dao.selectZxjh(wmsPicking.getSsfpid());
                                for (WmsBox box : wmsBoxList) {
                                    //查询到库存对象
                                    WmsStock wmsStock = dao.selectKc(wmsPicking.getKcid());
                                    wmsStock.setId(UUIDUtils.uuid());
                                    wmsStock.setKwid(owner.getJhqkwid());//把库位id换成集货区库位id
                                    wmsStock.setSl(String.valueOf(box.getSl()));//把实际装箱数量主单位 ,放到库存表的库存数量里
                                    wmsStock.setDxjsl("0");
                                    wmsStock.setDzysl("0");
                                    wmsStock.setDjsl("0");
                                    wmsStock.setPssl("0");
                                    wmsStock.setBhsl("0");
                                    map.put("WmsStock", wmsStock);
                                    Integer lhkw = dao.insetNewKc(map);//生成新库存
                                    if (lhkw > 0) {
                                    } else {
                                        throw new ServiceException("理货库存新增失败");
                                    }
                                    map2 = new HashMap<String, Object>();
                                    pick = new WmsPicking();
                                    pick.setId(UUIDUtils.uuid());
                                    pick.setSsfpid(wmsPicking.getSsfpid());
                                    pick.setSszxid(box.getId());
                                    pick.setSsydid(wmsShipmentOrder.getId());
                                    pick.setSsfymxid(wmsShipmentOrderDetailed.getId());
                                    pick.setKcid(wmsPicking.getKcid());
                                    pick.setSjsl(box.getSl());
                                    pick.setJhkwid(wmsPicking.getJhkwid());
                                    pick.setJihkwid(owner.getJhqkwid());
                                    pick.setFyzt("0");
                                    pick.setJhhkcid("0");
                                    pick.setCkid(ckid);
                                    pick.setJhhkcid(wmsStock.getId());
                                    map2.put("WmsPicking", pick);
                                    Integer jhls = dao.insertPick(map2);//添加拣货历史记录
                                    if (jhls > 0) {
                                        //把现在得到的总数,到库存表里用待下架数量和库存数量各自减掉去
                                        map = new HashMap<String, Object>();
                                        map.put("kcid", wmsPicking.getKcid());
                                        map.put("kcsl", box.getSl());
                                        Integer kcdj = dao.updateSl(map);
                                        if (kcdj > 0) {
                                        } else {
                                            throw new ServiceException("库存数量更改失败");
                                        }
                                    } else {
                                        throw new ServiceException("拣货历史记录添加失败");
                                    }
                                    Integer sl = Integer.valueOf(box.getSl());//箱里的主单位数量,也就是实际数量
                                    kcsl = sl + kcsl;//(一条分配对应一条库存,多个装箱的总数量) 循环叠加(把装箱里的实际数量循环取出来和定义的总数)
                                }
                                map.put("name", name);
                                map.put("ssfpid", wmsPicking.getSsfpid());
                                Integer fpjhr = dao.updateFp(map);//更改分配拣货人
                                if (fpjhr > 0) {
                                } else {
                                    throw new ServiceException("分配数据拣货人更改失败");
                                }
                                Integer zxjhr = dao.updateZh(map);//更改装箱拣货人
                                if (zxjhr > 0) {
                                } else {
                                    throw new ServiceException("装箱数据拣货人更改失败");
                                }
                                zsl = kcsl + zsl;//全部分配对应装箱的主单位数量,(一条明细对应全部装箱的主单位数量)zsl是最终用于判断的全部拣货还是部分拣货
                            }
                        } else {
                            WmsPicking picking = null;//生成拣货记录的对象
                            //拿着订单明细id 查分配表和库存的 联合集合数据
                            map = new HashMap<String, Object>();
                            map.put("ssfymxid", wmsShipmentOrderDetailed.getId());
                            map.put("sshzid", sshzid);
                            map.put("ckid", ckid);
                            map.put("jhr", "0");
                            List<WmsPicking> wmsPickingList2 = dao.seachKc_s(map);
                            for (WmsPicking wmsPicking : wmsPickingList2) {
                                map2 = new HashMap<String, Object>();
                                Integer sl = Integer.valueOf(wmsPicking.getFpsl());//箱里的主单位数量,也就是实际数量
                                zsl = sl + zsl;//循环叠加(把箱里的实际数量循环取出来和定义的总数),zsl这个总数用于判断是全拣货还是部分拣货的

                                //查询到库存对象
                                WmsStock wmsStock = dao.selectKc(wmsPicking.getKcid());
                                wmsStock.setId(UUIDUtils.uuid());
                                wmsStock.setKwid(owner.getJhqkwid());//把库位id换成集货区库位id
                                wmsStock.setSl(String.valueOf(sl));//把实际装箱数量主单位 ,放到库存表的库存数量里
                                wmsStock.setDxjsl("0");
                                wmsStock.setDzysl("0");
                                wmsStock.setDjsl("0");
                                wmsStock.setPssl("0");
                                wmsStock.setBhsl("0");
                                map2.put("WmsStock", wmsStock);
                                Integer lhkw = dao.insetNewKc(map2);//生成新库存
                                if (lhkw > 0) {
                                } else {
                                    throw new ServiceException("理货库存新增失败");
                                }
                                picking = new WmsPicking();
                                picking.setId(UUIDUtils.uuid());
                                picking.setSsfpid(wmsPicking.getSsfpid());
                                picking.setSszxid("0");
                                picking.setSsydid(wmsShipmentOrder.getId());
                                picking.setSsfymxid(wmsShipmentOrderDetailed.getId());
                                picking.setKcid(wmsPicking.getKcid());
                                picking.setSjsl(wmsPicking.getFpsl());
                                picking.setJhkwid(wmsPicking.getJhkwid());
                                picking.setJihkwid(owner.getJhqkwid());
                                picking.setFyzt("0");
                                picking.setJhhkcid("0");
                                picking.setCkid(ckid);
                                picking.setJhhkcid(wmsStock.getId());
                                map2.put("WmsPicking", picking);
                                Integer jhLs = dao.insertPick(map2);//添加拣货历史记录
                                Integer jhLs2 = 0;
                                if (jhLs > jhLs2) {
                                    map2.put("kcid", wmsPicking.getKcid());
                                    map2.put("kcsl", sl);
                                    Integer kcdj = dao.updateSl(map2);
                                    if (kcdj > 0) {
                                    } else {
                                        throw new ServiceException("库存数量更改失败");
                                    }
                                } else {
                                    throw new ServiceException("拣货历史记录添加失败");
                                }
                                map2.put("name", name);
                                map2.put("ssfpid", wmsPicking.getSsfpid());
                                Integer fpjhr = dao.updateFp(map2);//更改分配拣货人
                                if (fpjhr > 0) {
                                } else {
                                    throw new ServiceException("分配数据拣货人更改失败");
                                }
                                Integer zxjhr = dao.updateZh(map2);//更改装箱拣货人*/
                                if (zxjhr > 0) {
                                } else {
                                    throw new ServiceException("装箱数据拣货人更改失败");
                                }
                            }
                        }
                        // 用货品id查这个箱里的货品对应的包装明细类型为2的箱数量//在货品放这个箱里能放多少个
                        map = new HashMap<String, Object>();
                        map.put("hpid", wmsShipmentOrderDetailed.getHpid());
                        map.put("bzlx", "2");
                        WmsPackingDetailed detailed = dao.searchPackingDetailed(map);
                        map.put("bzlx", "0");
                        WmsPackingDetailed detailed2 = dao.searchPackingDetailed(map);
                        //每次累计明细里的拣货数量
                        zsl = zsl + Integer.parseInt(wmsShipmentOrderDetailed.getJhsl());
                        //箱主单位整数（箱数）
                        xdwzs = zsl / Integer.parseInt(detailed.getZdwsl());
                        //箱主单位余数（个数）
                        xdwys = zsl % Integer.parseInt(detailed.getZdwsl());
                        String jhxdwsl = xdwzs + detailed.getMs() + xdwys + detailed2.getMs();
                        Integer mxfyzt;
                        map = new HashMap<String, Object>();
                        map.put("id", wmsShipmentOrderDetailed.getId());
                        map.put("jhxdwsl", jhxdwsl);
                        //把现在得到的总数,拿着和订单明细里的数量进行比对,如果相等是全部检出,如果小于订单明细里的数量是部分检出
                        Integer fpsl = Integer.parseInt(wmsShipmentOrderDetailed.getFpsl());
                        if (zsl.equals(fpsl)) {
                            map.put("fymxzt", "4");
                            mxfyzt = dao.updateFymxzt(map);//改发运明细状态为全部拣货
                        } else {
                            flag = true;
                            map.put("fymxzt", "3");
                            mxfyzt = dao.updateFymxzt(map);//改发运明细状态为部分拣货
                        }
                        if (mxfyzt > 0) {
                        } else {
                            throw new ServiceException("发运明细状态更改失败");
                        }
                }
                            Integer ddfyzt;
                            map = new HashMap<String, Object>();
                            map.put("id", wmsShipmentOrder.getId());
                            if (flag != true) {
                                map.put("jhzt", "2");
                                ddfyzt = dao.updateFy(map);//发运订单里的状态改为全部拣货
                            } else {
                                map.put("jhzt", "1");
                                ddfyzt = dao.updateFy(map);//发运订单里的状态改为部分拣货
                            }
                            if (ddfyzt > 0) {
                            } else {
                                throw new ServiceException("发运状态更改失败");
                            }
                }
        }
            throw new SucessException("追加拣货成功");
      }else{
            throw new ServiceException("请选择货主管理中'是否允许多次发货'为允许的数据");
        }
    }

    /**
     * 查询发运订单信息(生成拣货任务专用)
     *
     * @param wmsPicking
     * @return
     * @throws Exception
     */
    public List<WmsPicking> pickingTaskList(WmsPicking wmsPicking) throws Exception {
        try {
            Map<String, Object> map = new HashMap();
            Map<String, Object> map1;
            //查询货主信息
            WmsPicking wmsPick = dao.sechSfqx(wmsPicking.getSshzid());
            //发运状态(1、部分分配2、全部分配)
            List<String> fyztList = new ArrayList<>();
            fyztList.add("1");
            fyztList.add("2");
            map.put("fyztList", fyztList);
            List<String> zxztList = null;
            //查看货主是否切箱
            if ("0".equals(wmsPick.getZxqx())) {
                zxztList = null;
            } else if ("1".equals(wmsPick.getZxqx())) {
                //装箱状态（0、未装箱 1、部分装箱 2、全部装箱）
                zxztList = new ArrayList<>();
                zxztList.add("1");
                zxztList.add("2");
            }
            map.put("zxztList", zxztList);
            //判断数据显示的内容
            List<WmsPicking> list = new ArrayList<>();
            map.put("WmsPicking", wmsPicking);
            List<WmsPicking> wmsPickingList = dao.pickingTaskList(map);
            for (WmsPicking wmsPicking2 : wmsPickingList) {
                map1 = new HashMap();
                map1.put("ssydid", wmsPicking2.getSsydid());
                //查询发运订单下的分配数据
                List<WmsDistribution> wmsDistributionList = dao.searchDistribution(map1);
                if (wmsDistributionList.size() > 0) {
                    list.add(wmsPicking2);
                }
            }
            if (list.size() > 0) {
                //开始的数量
                Integer start = wmsPicking.getRows() * (wmsPicking.getPage() - 1);
                //结束的数量
                Integer end = wmsPicking.getRows() * wmsPicking.getPage();
                if (end > list.size()) {
                    end = list.size();
                }
                list = list.subList(start, end);
            }
            return list;
        } catch (Exception e) {
            throw new ServiceException("查询发运订单信息失败", e);
        }
    }

    /**
     * 生成拣货任务信息(生成拣货任务专用)
     *
     * @param wmsPickingList
     * @param hzid
     * @param ckid
     */
    @BaseServiceDescribe(moduleName = "拣货任务", btnName = "生成拣货任务")
    @Transactional
    public void createPickingTask(List<WmsPicking> wmsPickingList, String hzid, String hzmc, String ckid) throws Exception {
        //记录pda添加数据(用于回滚)
        List<WmsPickingTaskPda> wmsPickingTaskPdaList1 = new ArrayList<>();
        //记录Dps添加数据(用于回滚)
        List<WmsPickingTaskDps> wmsPickingTaskDpsList1 = new ArrayList<>();
        //记录分配添加数据(用于回滚)
        List<WmsDistribution> wmsDistributionList1 = new ArrayList<>();
        //记录发运订单数据(用于回滚)
        List<WmsShipmentOrder> wmsShipmentOrderList1 = new ArrayList<>();
        //发运明细的发运状态(1、部分分配2、全部分配)
        List<String> fyztList = new ArrayList<>();
        fyztList.add("1");
        fyztList.add("2");
        WmsDistribution wmsDistribution = null;
        Map<String, Object> map = null;
        //查询货主信息
        WmsOwner wmsOwner = dao.searchWmsOwner(hzid);
        //是否装箱 0不装箱 1装箱
        String zxqx = wmsOwner.getZxqx();
        //是否对接DPS 0无对接 1已对接
        String djdps = wmsOwner.getDjdps();
        //是否对接PDA 0无对接 1已对接
        String djpda = wmsOwner.getDjpda();
        //(装箱,对接DPS,对接PDA)
        if ("1".equals(zxqx) && "1".equals(djdps) && "1".equals(djpda)) {
            //散装明细写入中间数据库,整箱明细写入到PDA拣货任务表(查询装箱表)
            for (WmsPicking wmsPicking : wmsPickingList) {
                WmsShipmentOrder wmsShipmentOrder = dao.selectDd(wmsPicking.getSsydid());
                //判断发运订单是否全部拣货:拣货状态（0、初始 1、部分拣货 2、全部拣货）
                if ("2".equals(wmsShipmentOrder.getJhzt())) {
                    black(wmsDistributionList1, wmsPickingTaskPdaList1, wmsPickingTaskDpsList1, map, wmsShipmentOrderList1);
                    throw new ServiceException("发运订单已全部拣货,不可生成拣货任务");
                } else {
                    //整散类型 0整箱 1散箱
                    String zslx = "0";
                    map = new HashMap<>();
                    map.put("zslx", zslx);
                    map.put("ydid", wmsPicking.getSsydid());
                    //查询发运订单下面的装箱的整箱数据
                    List<WmsPickingTaskPda> wmsPickingTaskPdaList = dao.searchBox(map);
                    //保存Pda
                    savePda(wmsPickingTaskPdaList, wmsPicking, wmsPickingTaskDpsList1, wmsDistribution, wmsDistributionList1, wmsPickingTaskPdaList1, wmsOwner, map, wmsShipmentOrderList1);
                    //整散类型 0整箱 1散箱
                    zslx = "1";
                    map = new HashMap<>();
                    map.put("zslx", zslx);
                    map.put("ydid", wmsPicking.getSsydid());
                    //查询发运订单下面的装箱的散箱数据
                    List<WmsPickingTaskDps> wmsPickingTaskDpsList = dao.searchBox2(map);
                    //保存DPS
                    saveDps(wmsPickingTaskDpsList, wmsPicking, wmsPickingTaskDpsList1, wmsDistribution, wmsDistributionList1, wmsPickingTaskPdaList1, map, wmsShipmentOrderList1, hzmc);
                    //更改发运订单拣货任务生成状态
                    updateShipmentOrder(wmsPickingTaskDpsList1, wmsDistributionList1, wmsPickingTaskPdaList1, map, wmsShipmentOrder, wmsShipmentOrderList1);
                }
            }
        } else if ("0".equals(djdps) && "0".equals(djpda)) {
            //无pda,无dps不能进行拣货任务操作
            throw new ServiceException("货主无对接PDA、无对接DPS不可进行拣货任务操作");
        } else if ("1".equals(zxqx) && ("1".equals(djdps) || "1".equals(djpda))) {
            //装箱(查询装箱表)
            for (WmsPicking wmsPicking : wmsPickingList) {
                WmsShipmentOrder wmsShipmentOrder = dao.selectDd(wmsPicking.getSsydid());
                //判断发运订单是否全部拣货:拣货状态（0、初始 1、部分拣货 2、全部拣货）
                if ("2".equals(wmsShipmentOrder.getJhzt())) {
                    black(wmsDistributionList1, wmsPickingTaskPdaList1, wmsPickingTaskDpsList1, map, wmsShipmentOrderList1);
                    throw new ServiceException("发运订单已全部拣货,不可生成拣货任务");
                } else {
                    //查询装箱明细
                    if ("1".equals(djdps)) {
                        map = new HashMap<>();
                        map.put("zslx", null);
                        map.put("ydid", wmsPicking.getSsydid());
                        //有DPS,保存进DPS(无PDA)
                        List<WmsPickingTaskDps> wmsPickingTaskDpsList = dao.searchBox2(map);
                        //保存DPS
                        saveDps(wmsPickingTaskDpsList, wmsPicking, wmsPickingTaskDpsList1, wmsDistribution, wmsDistributionList1, wmsPickingTaskPdaList1, map, wmsShipmentOrderList1, hzmc);
                    } else if ("1".equals(djpda)) {
                        map = new HashMap<>();
                        map.put("zslx", null);
                        map.put("ydid", wmsPicking.getSsydid());
                        //无DPS,保存进PDA
                        List<WmsPickingTaskPda> wmsPickingTaskPdaList = dao.searchBox(map);
                        //保存Pda
                        savePda(wmsPickingTaskPdaList, wmsPicking, wmsPickingTaskDpsList1, wmsDistribution, wmsDistributionList1, wmsPickingTaskPdaList1, wmsOwner, map, wmsShipmentOrderList1);
                    }
                    //更改发运订单拣货任务生成状态
                    updateShipmentOrder(wmsPickingTaskDpsList1, wmsDistributionList1, wmsPickingTaskPdaList1, map, wmsShipmentOrder, wmsShipmentOrderList1);
                }
            }
        } else if ("0".equals(zxqx) && ("1".equals(djdps) || "1".equals(djpda))) {
            for (WmsPicking wmsPicking : wmsPickingList) {
                WmsShipmentOrder wmsShipmentOrder = dao.selectDd(wmsPicking.getSsydid());
                //未装箱(查询分配表)
                map = new HashMap<>();
                map.put("fyztList", fyztList);
                map.put("ssfyddid", wmsPicking.getSsydid());
                List<WmsShipmentOrderDetailed> wmsShipmentOrderDetailedList = dao.orderDetailed(map);
                for (WmsShipmentOrderDetailed wmsShipmentOrderDetailed : wmsShipmentOrderDetailedList) {
                    map.put("fyddmxid", wmsShipmentOrderDetailed.getId());
                    //拣货任务生成状态 0：未生成 1：已生成
                    map.put("sczt", "0");
                    //拣货人(默认0)
                    map.put("jhr", "0");
                    //装箱状态 0未装箱 1已装箱
                    map.put("zxzt", "0");
                    if ("1".equals(djdps)) {
                        //查询发运订单下面的分配数据(DPS)
                        List<WmsPickingTaskDps> wmsDistributionList = dao.queryDistribution2(map);
                        //保存DPS
                        saveDps(wmsDistributionList, wmsPicking, wmsPickingTaskDpsList1, wmsDistribution, wmsDistributionList1, wmsPickingTaskPdaList1, map, wmsShipmentOrderList1, hzmc);
                    } else if ("1".equals(djpda)) {
                        //查询发运订单下面的分配数据(PDA)
                        List<WmsPickingTaskPda> wmsDistributionList = dao.queryDistribution(map);
                        //保存Pda
                        savePda(wmsDistributionList, wmsPicking, wmsPickingTaskDpsList1, wmsDistribution, wmsDistributionList1, wmsPickingTaskPdaList1, wmsOwner, map, wmsShipmentOrderList1);
                    }
                }
                //更改发运订单拣货任务生成状态
                updateShipmentOrder(wmsPickingTaskDpsList1, wmsDistributionList1, wmsPickingTaskPdaList1, map, wmsShipmentOrder, wmsShipmentOrderList1);
            }
        }
    }

    /**
     * 添加DPS数据(生成拣货任务专用)
     *
     * @param wmsPickingTaskDpsList
     * @param wmsPicking
     * @param wmsPickingTaskDpsList1
     * @param wmsDistribution
     * @param wmsDistributionList1
     * @param wmsPickingTaskPdaList1
     * @param map
     * @throws Exception
     */
    public void saveDps(List<WmsPickingTaskDps> wmsPickingTaskDpsList, WmsPicking wmsPicking, List<WmsPickingTaskDps> wmsPickingTaskDpsList1, WmsDistribution wmsDistribution, List<WmsDistribution> wmsDistributionList1, List<WmsPickingTaskPda> wmsPickingTaskPdaList1, Map map, List<WmsShipmentOrder> wmsShipmentOrderList1, String hzmc) throws Exception {
        for (WmsPickingTaskDps wmsPickingTaskDps : wmsPickingTaskDpsList) {
            map = new HashMap<>();
            //出库单号(装箱存放装箱编号)
            wmsPickingTaskDps.setOrderNo(wmsPickingTaskDps.getOrderNo2());
            //客户名称
            wmsPickingTaskDps.setReceName(wmsPicking.getKhmc());
            //收货方地址
            wmsPickingTaskDps.setReceAddr(wmsPicking.getKhdz());
            //联系人(姓名)
            wmsPickingTaskDps.setRecePerson(wmsPicking.getLxrxm());
            //联系电话
            wmsPickingTaskDps.setReceTel(wmsPicking.getLxrdh());
            //状态(0：初始 1：已读取)
            wmsPickingTaskDps.setState("0");
            //拣货任务id
            wmsPickingTaskDps.setId(UUIDUtils.uuid());
            map.put("WmsPickingTaskDps", wmsPickingTaskDps);
            //保存拣货任务进DPS
            Integer j = dao.createPickingTask2(map);
            if (j > 0) {
                wmsPickingTaskDpsList1.add(wmsPickingTaskDps);
                wmsDistribution = new WmsDistribution();
                wmsDistribution.setId(wmsPickingTaskDps.getDetailedId());
                //拣货任务生成状态 0：未生成 1：已生成
                wmsDistribution.setSczt("1");
                //拣货人
                wmsDistribution.setJhr(hzmc);
                map.put("WmsDistribution", wmsDistribution);
                //更改分配生成状态
                j = dao.updateFp2(map);
                if (j > 0) {
                    wmsDistributionList1.add(wmsDistribution);
                } else {
                    black(wmsDistributionList1, wmsPickingTaskPdaList1, wmsPickingTaskDpsList1, map, wmsShipmentOrderList1);
                    throw new ServiceException("分配数据生成状态更改失败");
                }
            } else {
                black(wmsDistributionList1, wmsPickingTaskPdaList1, wmsPickingTaskDpsList1, map, wmsShipmentOrderList1);
                throw new ServiceException("拣货任务DPS添加失败");
            }
        }
    }

    /**
     * 添加PDA数据(生成拣货任务专用)
     *
     * @param wmsPickingTaskPdaList
     * @param wmsPicking
     * @param wmsPickingTaskDpsList1
     * @param wmsDistribution
     * @param wmsDistributionList1
     * @param wmsPickingTaskPdaList1
     * @param wmsOwner
     * @param map
     * @throws Exception
     */
    public void savePda(List<WmsPickingTaskPda> wmsPickingTaskPdaList, WmsPicking wmsPicking, List<WmsPickingTaskDps> wmsPickingTaskDpsList1, WmsDistribution wmsDistribution, List<WmsDistribution> wmsDistributionList1, List<WmsPickingTaskPda> wmsPickingTaskPdaList1, WmsOwner wmsOwner, Map map, List<WmsShipmentOrder> wmsShipmentOrderList1) throws Exception {
        for (WmsPickingTaskPda wmsPickingTaskPda : wmsPickingTaskPdaList) {
            map = new HashMap<>();
            if ("0".equals(wmsOwner.getSmLpn())) {
                //清空LPN码(货主未扫描LPN)
                wmsPickingTaskPda.setMpbh("");
            }
            if ("0".equals(wmsOwner.getCmhpm())) {
                //清空货品码(货主未扫描货品码)
                wmsPickingTaskPda.setHpm("");
            } else {
                //货品编号放入货品码
                wmsPickingTaskPda.setHpm(wmsPickingTaskPda.getHpbh());
            }
            //拣货任务id
            wmsPickingTaskPda.setId(UUIDUtils.uuid());
            //发运订单id
            wmsPickingTaskPda.setFyid(wmsPicking.getSsydid());
            //发运编号
            wmsPickingTaskPda.setFybh(wmsPicking.getFybh());
            //联系人id
            wmsPickingTaskPda.setRyid(wmsPicking.getRyid());
            //货主id
            wmsPickingTaskPda.setSshzid(wmsOwner.getId());
            //仓库id
            wmsPickingTaskPda.setCkid(wmsOwner.getCkid());
            //集货库位id
            wmsPickingTaskPda.setJhkwid(wmsOwner.getJhqkwid());
            //集货库位名称
            wmsPickingTaskPda.setJhkw(wmsOwner.getShow_jhqkw());
            map.put("WmsPickingTaskPda", wmsPickingTaskPda);
            //保存拣货任务进pda
            Integer i = dao.createPickingTask(map);
            if (i > 0) {
                wmsPickingTaskPdaList1.add(wmsPickingTaskPda);
                wmsDistribution = new WmsDistribution();
                wmsDistribution.setId(wmsPickingTaskPda.getFpid());
                //拣货任务生成状态 0：未生成 1：已生成
                wmsDistribution.setSczt("1");
                map.put("WmsDistribution", wmsDistribution);
                //更改分配生成状态
                i = dao.updateFp2(map);
                if (i > 0) {
                    wmsDistributionList1.add(wmsDistribution);
                } else {
                    black(wmsDistributionList1, wmsPickingTaskPdaList1, wmsPickingTaskDpsList1, map, wmsShipmentOrderList1);
                    throw new ServiceException("分配数据生成状态更改失败");
                }
            } else {
                black(wmsDistributionList1, wmsPickingTaskPdaList1, wmsPickingTaskDpsList1, map, wmsShipmentOrderList1);
                throw new ServiceException("拣货任务PDA添加失败");
            }
        }
    }

    /**
     * 生成拣货任务 保存拣货信息(dps回传)
     *
     * @param wmsPickingTaskDps
     */
    public void savePicking(WmsPickingTaskDps wmsPickingTaskDps) throws Exception {
        Map<String, Object> map = new HashMap<>();
        //查询分配信息(传分配id)
        WmsDistribution wmsDistribution = dao.searchFpMx(wmsPickingTaskDps.getDetailedId());
        if (wmsDistribution != null) {
            //查询发运订单明细数据(发运订单明细id)
            WmsShipmentOrderDetailed wmsShipmentOrderDetailed = dao.searchDdMx(wmsDistribution.getSsfymxid());
            WmsShipmentOrder wmsShipmentOrder = dao.searchDd(wmsShipmentOrderDetailed.getSsfyddid());
            //查询货主信息
            WmsPicking owner = dao.sechSfqx(wmsShipmentOrder.getSshzid());
            //保存拣货数据
            WmsPicking wmsPicking = new WmsPicking();
            //ID
            wmsPicking.setId(UUIDUtils.uuid());
            //所属分配ID
            wmsPicking.setSsfpid(wmsPickingTaskDps.getDetailedId());
            //“是否切箱”  0不切箱 1切箱
            if ("1".equals(owner.getZxqx())) {
                //查询装箱信息(分配ID)
                WmsBox wmsBox = dao.searchBox3(wmsPicking.getSsfpid());
                if (wmsBox != null) {
                    wmsPicking.setSszxid(wmsBox.getId());
                }
            } else {
                //装箱ID(默认0)
                wmsPicking.setSszxid("0");
            }

            //发运订单ID
            wmsPicking.setSsydid(wmsShipmentOrderDetailed.getSsfyddid());
            //发运订单明细ID
            wmsPicking.setSsfymxid(wmsShipmentOrderDetailed.getId());
            //库存ID
            wmsPicking.setKcid(wmsDistribution.getKcid());
            //拣货数量(实捡数量)
            wmsPicking.setSjsl(String.valueOf(wmsPickingTaskDps.getNum()));
            //拣货库位ID
            wmsPicking.setJhkwid(wmsDistribution.getShow_kwid());
            //集货库位ID
            wmsPicking.setJihkwid(owner.getJhqkwid());
            //发运状态 0未发运 1已发运
            wmsPicking.setFyzt("0");
            //拣货后库存ID
            wmsPicking.setJhhkcid("0");
            //仓库id
            wmsPicking.setCkid(wmsShipmentOrderDetailed.getCkid());
            map.put("WmsPicking", wmsPicking);
            Integer i = dao.insertPick(map);//添加拣货信息
            if (i > 0) {
                WmsDistribution wmsDistribution1 = new WmsDistribution();
                wmsDistribution1.setJhsj(wmsPickingTaskDps.getDownLoadDate());
                //拣货任务生成状态 0：未生成 1：已生成
                wmsDistribution1.setSczt("1");
                //分配id
                wmsDistribution1.setId(wmsPickingTaskDps.getDetailedId());
                map.put("WmsDistribution", wmsDistribution1);
                //更改分配表
                i = dao.updateFp2(map);
                if (i > 0) {
                    //查询到库存对象
                    WmsStock wmsStock = dao.selectKc(wmsDistribution.getKcid());
                    wmsStock.setId(UUIDUtils.uuid());
                    //集货区库位id
                    wmsStock.setKwid(owner.getJhqkwid());
                    //拣货数量
                    wmsStock.setSl(String.valueOf(wmsPickingTaskDps.getNum()));
                    map.put("WmsStock", wmsStock);
                    //生成新库存
                    i = dao.insetNewKc(map);
                    if (i > 0) {
                        map.put("kcid", wmsDistribution.getKcid());
                        map.put("kcsl", String.valueOf(wmsPickingTaskDps.getNum()));
                        //更改原库存数量
                        i = dao.updateSl(map);
                        if (i > 0) {
                            map.put("hpid", wmsShipmentOrderDetailed.getHpid());
                            map.put("bzlx", "2");
                            //箱单位
                            WmsPackingDetailed detailed = dao.searchPackingDetailed(map);
                            map.put("bzlx", "0");
                            //主单位
                            WmsPackingDetailed detailed2 = dao.searchPackingDetailed(map);
                            Integer zsl = Integer.parseInt(wmsStock.getSl());
                            if ("".equals(wmsShipmentOrderDetailed.getJhsl()) || wmsShipmentOrderDetailed.getJhsl() == null) {
                                wmsShipmentOrderDetailed.setJhsl("0");
                            }
                            //数量(拣货数量+发运订单明细的分配数量)
                            zsl = zsl + Integer.parseInt(wmsShipmentOrderDetailed.getJhsl());
                            //箱主单位整数（箱数）
                            Integer xdwzs = zsl / Integer.parseInt(detailed.getZdwsl());
                            //箱主单位余数（个数）
                            Integer xdwys = zsl % Integer.parseInt(detailed.getZdwsl());
                            String jhxdwsl = xdwzs + detailed.getMs() + xdwys + detailed2.getMs();
                            map.put("id", wmsShipmentOrderDetailed.getId());
                            map.put("jhxdwsl", jhxdwsl);
                            map.put("zsl", zsl);
                            //取得发运订单明细分配数量
                            Integer fpsl = Integer.parseInt(wmsShipmentOrderDetailed.getFpsl());
                            if (zsl.equals(fpsl)) {
                                //4、全部拣货
                                map.put("fymxzt", "4");
                            } else {
                                //3、部分拣货
                                map.put("fymxzt", "3");
                            }
                            //改发运明细状态为部分拣货
                            i = dao.updateFymxzt(map);
                            if (i > 0) {
                                map.put("id", wmsShipmentOrderDetailed.getSsfyddid());
                                //统计发运订单明细数据
                                WmsShipmentOrderDetailed wmsShipmentOrderDetailed2 = dao.searchDdMx2(map);
                                if (wmsShipmentOrderDetailed2.getFpsl().equals(wmsShipmentOrderDetailed2.getJhsl())) {
                                    //全部拣货
                                    map.put("jhzt", "2");
                                } else {
                                    //部分拣货
                                    map.put("jhzt", "1");
                                }
                                //发运订单里的状态改为全部拣货
                                i = dao.updateFy(map);
                                if (i > 0) {
                                    throw new SucessException("拣货任务操作成功");
                                } else {
                                    throw new ServiceException("发运状态更改失败");
                                }
                            } else {
                                throw new ServiceException("发运明细状态更改失败");
                            }
                        } else {
                            throw new ServiceException("库存更改失败");
                        }
                    } else {
                        throw new ServiceException("库存新增失败");
                    }
                } else {
                    throw new ServiceException("更改分配表失败");
                }
            } else {
                throw new ServiceException("拣货信息添加失败");
            }
        } else {
            throw new ServiceException("分配明细ID数据有误");
        }
    }

    /**
     * 打印分拣单查询
     * @param wmsPrintPicking
     * @return
     */
    public List<WmsPrintPicking> printPicking(WmsPrintPicking wmsPrintPicking) throws Exception {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> map2 = null;
        map.put("WmsPrintPicking", wmsPrintPicking);
        List<WmsPrintPicking> wmsPrintPickingList = dao.printPicking(map);
        for (WmsPrintPicking wmsPrintPicking1 : wmsPrintPickingList) {
            /*//发运状态(0、初始 1、部分分配2、全部分配 3、部分发运4、全部发运5、订单取消6、订单完成)
            if (!"1".equals(wmsPrintPicking1.getFyzt()) && !"2".equals(wmsPrintPicking1.getFyzt())) {
                throw new ServiceException("请选择订单状态为部分分配或者全部分配的订单");
            } else {
                //存取拣货任务状态(0.初始,1.部分生成拣货任务,2.全部生成拣货任务)
                if (!"0".equals(wmsPrintPicking1.getJhrwzt()) && !"1".equals(wmsPrintPicking1.getJhrwzt())) {
                    throw new ServiceException("请选择生成状态为初始或者部分生成的订单");
                } else {
                    //存取拣货状态（0、初始 1、部分拣货 2、全部拣货）
                    if (!"0".equals(wmsPrintPicking1.getJhzt()) && !"1".equals(wmsPrintPicking1.getJhzt())) {
                        throw new ServiceException("请选择拣货状态为初始或者部分拣货的订单");
                    } else {*/
                        /*WmsPrintPicking wmsPrintPicking3 = dao.printPicking2(wmsPrintPicking1.getShippingId());
                        wmsPrintPicking1.setMainUnit(wmsPrintPicking3.getMainUnit());*/
                        List<WmsPrintPickingDetail> wmsPrintPickingDetail = dao.printPickingDetailed(wmsPrintPicking1.getShippingId());
                        for (WmsPrintPickingDetail wmsPrintPickingDetail1 : wmsPrintPickingDetail) {
                            //计算拣货数量(箱单位)
                            map2 = new HashMap<String, Object>();
                            map2.put("hpid", wmsPrintPickingDetail1.getGoodsId());
                            map2.put("bzlx", "2");
                            WmsPackingDetailed detailed = dao.searchPackingDetailed(map2);
                            map2.put("bzlx", "0");
                            WmsPackingDetailed detailed2 = dao.searchPackingDetailed(map2);
                            Integer total = Integer.parseInt(wmsPrintPickingDetail1.getPickMainUnit());
                            //箱主单位整数（箱数）
                            Integer xdwzs = total / Integer.parseInt(detailed.getZdwsl());
                            //箱主单位余数（个数）
                            Integer xdwys = total % Integer.parseInt(detailed.getZdwsl());
                            String jhxdwsl = xdwzs + detailed.getMs() + xdwys + detailed2.getMs();
                            //拣货数量(箱单位)
                            wmsPrintPickingDetail1.setPickBoxUnit(jhxdwsl);
                        }
                        wmsPrintPicking1.setWmsPrintPickingDetailList(wmsPrintPickingDetail);
                    }
               /* }
            }
        }*/
        return wmsPrintPickingList;
    }

    /**
     * 打印合并分拣单查询
     *
     * @param wmsPrintPicking
     * @return
     */
    public WmsPrintPicking printMergePicking(WmsPrintPicking wmsPrintPicking) throws Exception {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> map2 = null;
        Map<String, Object> map3 = null;
        map.put("WmsPrintPicking", wmsPrintPicking);
        //查询合并发运订单
        WmsPrintPicking wmsPrintPicking1 = dao.printMergePicking(map);
        //获取货主集货库位
        WmsOwner wmsOwner = dao.searchWmsOwner2(wmsPrintPicking.getHzid());
        //打印合并分拣单明细查询
        List<WmsPrintPickingDetail> wmsPrintPickingDetail = dao.printMergePickingDetailed(map);
        //记录备注
        List<String> stringList = new ArrayList<>();
        StringBuffer stringBuffer = null;
        //查询备注信息
        List<WmsPrintPickingDetail> wmsPrintPickingDetaiList2 = dao.printMergePickingDetailed2(map);
        for (WmsPrintPickingDetail wmsPrintPickingDetail1 : wmsPrintPickingDetail) {
            for (WmsPrintPickingDetail wmsPrintPickingDetail2 : wmsPrintPickingDetaiList2) {
                if (wmsPrintPickingDetail1.getLibraryId().equals(wmsPrintPickingDetail2.getLibraryId()) && wmsPrintPickingDetail1.getGoodsNum().equals(wmsPrintPickingDetail2.getGoodsNum()) && wmsPrintPickingDetail1.getGoodsBatch().equals(wmsPrintPickingDetail2.getGoodsBatch()) && wmsPrintPickingDetail1.getProductionTime().equals(wmsPrintPickingDetail2.getProductionTime()) && wmsPrintPickingDetail1.getSupplierId().equals(wmsPrintPickingDetail2.getSupplierId())) {
                    stringBuffer = new StringBuffer();
                    //发运编号
                    stringBuffer.append(wmsPrintPickingDetail2.getFybh());
                    stringBuffer.append("/");
                    //客户名称
                    stringBuffer.append(wmsPrintPickingDetail2.getKhmc());
                    stringBuffer.append("/");
                    //分配数量
                    stringBuffer.append(wmsPrintPickingDetail2.getFpsl());
                    stringBuffer.append("/");
                    //集货区库位
                    stringBuffer.append(wmsOwner.getShow_jhqkw());
                    stringList.add(stringBuffer.toString());
                }
            }
            wmsPrintPickingDetail1.setStringList(stringList);
            //计算拣货数量(箱单位)
            map2 = new HashMap<String, Object>();
            map2.put("hpid", wmsPrintPickingDetail1.getGoodsId());
            map2.put("bzlx", "2");
            WmsPackingDetailed detailed = dao.searchPackingDetailed(map2);
            map2.put("bzlx", "0");
            WmsPackingDetailed detailed2 = dao.searchPackingDetailed(map2);
            Integer total = Integer.parseInt(wmsPrintPickingDetail1.getPickMainUnit());
            //箱主单位整数（箱数）
            Integer xdwzs = total / Integer.parseInt(detailed.getZdwsl());
            //箱主单位余数（个数）
            Integer xdwys = total % Integer.parseInt(detailed.getZdwsl());
            String jhxdwsl = xdwzs + detailed.getMs() + xdwys + detailed2.getMs();
            //拣货数量(箱单位)
            wmsPrintPickingDetail1.setPickBoxUnit(jhxdwsl);
        }
        wmsPrintPicking1.setWmsPrintPickingDetailList(wmsPrintPickingDetail);
        return wmsPrintPicking1;
    }
    /**
     * 查看已完成拣货任务
     *
     * @param hzid
     * @param ckid
     * @param fyddid
     * @return
     */
    public List<WmsPickingTask> seeCompleteTask(String hzid, String ckid, String fyddid) {
        Map<String, Object> map = new HashMap<>();
        map.put("hzid", hzid);
        map.put("ckid", ckid);
        map.put("fyddid", fyddid);
        return dao.seeCompleteTask(map);
    }

    /**
     * 查看未完成拣货任务
     *
     * @param hzid
     * @param ckid
     * @param fyddid
     * @return
     */
    public List<WmsPickingTask> notCompleteTask(String hzid, String ckid, String fyddid) {
        Map<String, Object> map = new HashMap<>();
        map.put("hzid", hzid);
        map.put("ckid", ckid);
        map.put("fyddid", fyddid);
        return dao.notCompleteTask(map);
    }

    /**
     * 更改发运订单状态
     *
     * @param wmsShipmentOrder
     * @param wmsShipmentOrderList1
     */
    public void updateShipmentOrder(List<WmsPickingTaskDps> wmsPickingTaskDpsList1, List<WmsDistribution> wmsDistributionList1, List<WmsPickingTaskPda> wmsPickingTaskPdaList1, Map map, WmsShipmentOrder wmsShipmentOrder, List<WmsShipmentOrder> wmsShipmentOrderList1) throws Exception {
        map = new HashMap();
        //拣货任务状态(0.初始,1.部分生成拣货任务,2.全部生成拣货任务)
        String jhrwzt = "2";
        map.put("jhrwzt", jhrwzt);
        map.put("id", wmsShipmentOrder.getId());
        //更改发运订单状态
        Integer i = dao.updateDdjhzt(map);
        if (i > 0) {
            wmsShipmentOrder.setJhrwzt(jhrwzt);
            wmsShipmentOrderList1.add(wmsShipmentOrder);
        } else {
            black(wmsDistributionList1, wmsPickingTaskPdaList1, wmsPickingTaskDpsList1, map, wmsShipmentOrderList1);
            throw new ServiceException("更改拣货任务状态失败");
        }
    }
    /**
     * 出错回滚(拣货任务专用)
     * @param wmsDistributionList
     * @param wmsPickingTaskPdaList
     * @param wmsPickingTaskDpsList
     */
    public void black(List<WmsDistribution> wmsDistributionList, List<WmsPickingTaskPda> wmsPickingTaskPdaList, List<WmsPickingTaskDps> wmsPickingTaskDpsList, Map map, List<WmsShipmentOrder> wmsShipmentOrderList1) {
        if (wmsDistributionList.size() > 0) {
            //还原分配生成状态
            for (WmsDistribution wmsDistribution : wmsDistributionList) {
                map = new HashMap();
                map.put("WmsDistribution", wmsDistribution);
                dao.updateFp2(map);
            }
        }
        if (wmsPickingTaskPdaList.size() > 0) {
            map = new HashMap();
            map.put("wmsPickingTaskPdaList", wmsPickingTaskPdaList);
            //还原PDA数据(删除pda数据)
            dao.deletePda(map);
        }
        if (wmsPickingTaskDpsList.size() > 0) {
            map = new HashMap();
            map.put("wmsPickingTaskDpsList", wmsPickingTaskDpsList);
            //还原DPS数据(删除dps数据)
            dao.deleteDps(map);
        }
        if (wmsShipmentOrderList1.size() > 0) {
            for (WmsShipmentOrder wmsShipmentOrder : wmsShipmentOrderList1) {
                map = new HashMap();
                map.put("id", wmsShipmentOrder.getId());
                map.put("jhrwzt", wmsShipmentOrder.getJhrwzt());
                //还原发运订单拣货任务状态
                dao.updateDdjhzt(map);
            }
        }
    }

    //********************************发运********************************************
         /**
          * 发运
          * @param sshzid
          * @param userinfo
          * @param list
          * @throws ServiceException
          */
        @BaseServiceDescribe(moduleName = "发运管理",btnName = "发运")
        public void despacthWmsicking( String sshzid,UserInfo userinfo,List<String> list) throws Exception{
            //查询货主信息
            WmsOwner wmsOwner = dao.searchWmsOwner(sshzid);
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//取仓库id
            Map<String,Object> map =null;
            Map<String, Object> map1 = null;
            Map<String,Object> map2 =null;
            for (String id : list) {
                //查询发运订单数据
                WmsShipmentOrder wmsShipmentOrder=dao.selectDd(id);
                //拣货状态（0、初始 1、部分拣货 2、全部拣货）
                String jhzt="1";
                String jhzt2="2";
                if(!jhzt.equals(wmsShipmentOrder.getJhzt()) && !jhzt2.equals(wmsShipmentOrder.getJhzt())){
                    throw new ServiceException("请选择拣货状态为部分拣货或者全部拣货的发运订单数据");
                }else{
                    //状态(0、初始 1、部分分配2、全部分配 3、部分发运4、全部发运5、订单取消6、订单完成)
                    String fyzt="1";
                    String fyzt2="2";
                    if(!fyzt.equals(wmsShipmentOrder.getFyzt()) && !fyzt2.equals(wmsShipmentOrder.getFyzt())){
                        throw new ServiceException("请选择发运订单状态是部分分配或全部分配的发运订单数据");
                    }else{
                        //用于标记发运订单明细发运状态(true为部分发运,false为全部发运)
                        boolean flag=true;
                        map=new HashMap<>();
                        map.put("ssfyddid",wmsShipmentOrder.getId());
                        map.put("fymxzt","");
                        //查询发运订单明细
                        List<WmsShipmentOrderDetailed> wmsShipmentOrderDetailedList = dao.searchOrderDetailed(map);
                         for (WmsShipmentOrderDetailed wmsShipmentOrderDetailed : wmsShipmentOrderDetailedList) {
                             //用来比对明细数量的,判断部分发运还是全部发运
                             Integer zsl = 0;
                             map =new HashMap<String,Object>();
                             map.put("ssfymxid",wmsShipmentOrderDetailed.getId());
                             map.put("ckid",ckid);
                             //发运状态 0、初始
                             map.put("fyzt","0");
                             //发运任务下发状态 0：初始 1：已下发
                             map.put("xfzt", "0");
                             //查拣货集合
                             List<WmsPicking> wmsPickingList =  dao.seachJh(map);
                             for (WmsPicking wmsPicking : wmsPickingList) {
                                 //出库复核 0否 1是
                                 if ("0".equals(wmsOwner.getCkfh())) {
                                     zsl += wmsickingFz(zsl, wmsPicking, map2);
                                 } else if ("1".equals(wmsOwner.getCkfh())) {
                                     //复核状态 0：未复核 1：已复核
                                     if ("1".equals(wmsPicking.getFhzt())) {
                                         zsl += wmsickingFz(zsl, wmsPicking, map2);
                                     } else if (!"0".equals(wmsPicking.getSszxid()) && "0" != wmsPicking.getSszxid()) {
                                         //整箱的拣货记录将不需要复核就可以发运(查询是否为整箱)
                                         WmsBox wmsBox = dao.searchBox4(wmsPicking.getSszxid());
                                         //整散类型 0整箱 1散箱
                                         if ("0".equals(wmsBox.getZslx())) {
                                             zsl += wmsickingFz(zsl, wmsPicking, map2);
                                         }
                                     }

                                 }
                             }
                             //发运总数量
                             Integer jhsl=Integer.valueOf(wmsShipmentOrderDetailed.getJhsl());
                             //货品对应的箱包装zdwsl对应主单位数量
                             map.put("hpid",wmsShipmentOrderDetailed.getHpid());
                             map.put("bzlx","2");
                             WmsPackingDetailed detailed =dao.searchPackingDetailed(map);
                             map.put("bzlx","0");
                             WmsPackingDetailed detailed2 =dao.searchPackingDetailed(map);
                             //箱主单位整数（箱数）
                             Integer xdwzs = zsl / Integer.parseInt(detailed.getZdwsl());
                             //箱主单位余数（个数）
                            Integer xdwys = zsl % Integer.parseInt(detailed.getZdwsl());
                             //明细fyxdwsl内的（发运）箱单位数量
                            String fyxdwsl = xdwzs + detailed.getMs() + xdwys + detailed2.getMs();
                             String fymxzt;
                             if(zsl<jhsl){
                                 map.put("fymxzt","5");
                                 flag=false;
                             }else{
                                 map.put("fymxzt","6");
                             }
                             map.put("mxid",wmsShipmentOrderDetailed.getId());
                             map.put("fyxdwsl",fyxdwsl);
                             map.put("zsl",zsl);
                            Integer mx=dao.updateMxfyzt(map);
                           if(mx>0){
                             }else{
                                throw new ServiceException("发运失败");
                             }
                         }
                        //改订单状态
                        String fyddzt;
                        if(flag){
                            //全部发运
                            map.put("fyddzt", "4");
                        }else{
                            //部分发运
                            map.put("fyddzt", "3");
                        }
                        map.put("orderid",wmsShipmentOrder.getId());
                        Integer dd=dao.updateDdfyzt(map);
                        if(dd>0){
                        }else{
                            throw new ServiceException("发运失败");
                        }
                    }
                }
            }
            throw new SucessException("发运成功");
        }

    /**
     * 辅助
     *
     * @param zsl
     * @param wmsPicking
     * @param map2
     * @return Integer
     * @throws Exception
     */
    public Integer wmsickingFz(Integer zsl, WmsPicking wmsPicking, Map<String, Object> map2) throws Exception {
        //叠加实捡数量
        zsl = Integer.parseInt(wmsPicking.getSjsl()) + zsl;
        //用库存id和集货库位id查 新库存返回库存对象
        map2 = new HashMap<String, Object>();
        map2.put("jhhkcid", wmsPicking.getJhhkcid());
        map2.put("jihkwid", wmsPicking.getJihkwid());
        WmsStock wmsStock = dao.selectStock(map2);
        if (wmsStock != null) {
            map2.put("id", wmsPicking.getId());
            //1、部分分配
            map2.put("fyzt", "1");
            //更改拣货表发运状态为已发运
            Integer i = dao.updateJhzt(map2);
            if (i > 0) {
                map2.put("scid", wmsStock.getId());
                map2.put("sl", "0");
                Integer j = dao.updateNewKcsl(map2);//再更改新库存的数量
                Integer j2 = 0;
                if (j > j2) {
                } else {
                    throw new ServiceException("更改理货库位数量失败");
                }
            } else {
                throw new ServiceException("拣货表发运状态更改失败");
            }
        } else {
            throw new ServiceException("理货库位数量为空");
        }
        return zsl;
    }
    /**
     * 追加发运
     * @param sshzid
     * @param userinfo
     * @param list
     * @throws ServiceException
     */
    @BaseServiceDescribe(moduleName = "追加发运管理",btnName = "追加发运")
    public void despacthAddWmsicking( String sshzid,UserInfo userinfo,List<String> list) throws Exception {
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//取仓库id
        //查询货主信息
        WmsOwner wmsOwner = dao.searchWmsOwner(sshzid);
        Map<String, Object> map = null;
        Map<String, Object> map2 = null;
        //判断货主管理中“是否切箱” zxqx 0不切箱 1切箱,  dcfh是否允许多次发货 0不允许 1允许 默认1
        WmsPicking owner = dao.sechSfqx(sshzid);
        if ("1".equals(owner.getDcfh())) {
            for (String id : list) {
                //发运订单
                WmsShipmentOrder wmsShipmentOrder = dao.selectDd(id);
                //3、部分发运
                String fyzt = "3";
                if (!fyzt.equals(wmsShipmentOrder.getFyzt())) {
                    throw new ServiceException("请选择发运订单状态是部分发运的发运订单数据");
                } else {
                    //用于标记发运订单明细发运状态(true为部分发运,false为全部发运)
                    boolean flag = true;
                    //查询发运订单明细用订单id
                    map = new HashMap<>();
                    map.put("ssfyddid", wmsShipmentOrder.getId());
                    //5、部分发运
                    map.put("fymxzt", "5");
                    //发运订单明细查询
                    List<WmsShipmentOrderDetailed> wmsShipmentOrderDetailedList = dao.searchOrderDetailed(map);
                    for (WmsShipmentOrderDetailed wmsShipmentOrderDetailed : wmsShipmentOrderDetailedList) {
                        Integer zsl = 0;//用来比对明细数量的,判断部分发运还是全部发运
                        //拿订单明细id查拣货集合
                        map = new HashMap<String, Object>();
                        map.put("ssfymxid", wmsShipmentOrderDetailed.getId());
                        map.put("ckid", ckid);
                        //发运状态 0初始 1复核成功 2复核失败
                        map.put("fyzt", "0");
                        //发运任务下发状态 0：初始 1：已下发
                        map.put("xfzt", "0");
                        //查拣货集合
                        List<WmsPicking> wmsPickingList = dao.seachJh(map);
                        for (WmsPicking wmsPicking : wmsPickingList) {
                            //出库复核 0否 1是
                            if ("0".equals(wmsOwner.getCkfh())) {
                                zsl += wmsickingFz(zsl, wmsPicking, map2);
                            } else if ("1".equals(wmsOwner.getCkfh())) {
                                //复核状态 0：未复核 1：已复核
                                if ("1".equals(wmsPicking.getFhzt())) {
                                    zsl += wmsickingFz(zsl, wmsPicking, map2);
                                } else if (!"0".equals(wmsPicking.getSszxid()) && "0" != wmsPicking.getSszxid()) {
                                    //整箱的拣货记录将不需要复核就可以发运(查询是否为整箱)
                                    WmsBox wmsBox = dao.searchBox4(wmsPicking.getSszxid());
                                    //整散类型 0整箱 1散箱
                                    if ("0".equals(wmsBox.getZslx())) {
                                        zsl += wmsickingFz(zsl, wmsPicking,map2);
                                    }
                                }

                            }
                        }
                        //发运总数量
                        Integer jhsl = Integer.valueOf(wmsShipmentOrderDetailed.getJhsl());
                        //每次累计明细里的拣货数量
                        zsl = zsl + Integer.parseInt(wmsShipmentOrderDetailed.getFysl());
                        //货品对应的箱包装zdwsl对应主单位数量
                        map.put("hpid", wmsShipmentOrderDetailed.getHpid());
                        map.put("bzlx", "2");
                        WmsPackingDetailed detailed = dao.searchPackingDetailed(map);
                        map.put("bzlx", "0");
                        WmsPackingDetailed detailed2 = dao.searchPackingDetailed(map);
                        //箱主单位整数（箱数）
                        Integer xdwzs = zsl / Integer.parseInt(detailed.getZdwsl());
                        //箱主单位余数（个数）
                        Integer xdwys = zsl % Integer.parseInt(detailed.getZdwsl());
                        //明细fyxdwsl内的（发运）箱单位数量
                        String jhxdwsl = xdwzs + detailed.getMs() + xdwys + detailed2.getMs();
                        String fymxzt;
                        if (zsl < jhsl) {
                            map.put("fymxzt", "5");
                            flag = false;
                        } else {
                            map.put("fymxzt", "6");
                        }
                        map.put("mxid", wmsShipmentOrderDetailed.getId());
                        map.put("jhxdwsl", jhxdwsl);
                        Integer mx = dao.updateMxfyzt(map);
                        if (mx > 0) {
                        } else {
                            throw new ServiceException("追加发运失败");
                        }
                    }
                    //改订单状态
                    String fyddzt;
                    if (flag) {
                        //全部发运
                        map.put("fyddzt", "4");
                    } else {
                        //部分发运
                        map.put("fyddzt", "3");
                    }
                    map.put("orderid", wmsShipmentOrder.getId());
                    Integer dd = dao.updateDdfyzt(map);
                    if (dd > 0) {
                    } else {
                        throw new ServiceException("追加发运失败");
                    }
                }
            }
            throw new SucessException("追加发运成功");
        }else{
            throw new ServiceException("请选择货主管理中'是否允许多次发货'为允许的数据");
        }
    }
}
